package com.sdzee.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.*;

import model.User;

@Stateless
public class UserDaoEJB {
	
	//Injection du Manager qui s'occupe de la connexion avec la BDD
		@PersistenceContext (unitName = "mydb_PU")
		private EntityManager em;
		
		
		
		
		
		public void add(User user) throws DAOException {
			try{			
				em.persist(user);

			} catch(Exception e){throw new DAOException (e);}
			}
		
		@SuppressWarnings("unchecked")
		public boolean notExistBis(String pseudo, String password){
			
			List<User> list = new ArrayList<User>() ;
			Query q = em.createQuery("select u from User u where u.pseudo=:N and u.password=:M");
			q.setParameter("N", pseudo);
			q.setParameter("M", password);
			try{
				list = (List<User>) q.getResultList();
				return list.isEmpty();
			} catch(Exception e){throw new DAOException (e);}
		}
		
		public boolean notExist(String pseudo){
			List<User> list = new ArrayList<User>() ;
			Query q = em.createQuery("select u from User u where u.pseudo=:N");
			q.setParameter("N", pseudo);			
			try{
				list = (List<User>) q.getResultList();
				return list.isEmpty();
			} catch(Exception e){throw new DAOException (e);}
		}
		
		@SuppressWarnings("unchecked")
		public List<User> find(String pseudo){
			
			List<User> list = new ArrayList<User>() ;
			Query q = em.createQuery("select u from Event u where u.pseudo =: N");
			q.setParameter("N", pseudo);
			try{
				list = (ArrayList<User>) q.getResultList();
				return list;
			} catch(Exception e){throw new DAOException (e);}
		}
		public List<String> findAll(){
			List<String> list = new ArrayList<String>() ;
			Query q = em.createQuery("select u.pseudo from User u");
			list = (List<String>)q.getResultList();
			return list;
		}

}
